{extend name="layout/base" /}
<!-- 标题 -->
{block name="title"}{$site['site_name']} - 栏目管理{/block}
<!-- CSS -->
{block name="css"}
    {load href="/static/lib/treetable/jquery.treetable.css"}
    {load href="/static/lib/chosen/chosen.min.css"}
    {load href="/static/lib/chosenicons/zui.chosenicons.min.css"}
{/block}
<!-- 具体内容 -->
{block name="content"}
<h2 class="header-dividing">{$site['site_name']} <small>栏目管理</small></h2>
<div class="navbar navbar-default navbar-content">
    <div class="collapse navbar-collapse">
        <ul class="nav navbar-nav">
            {if(check_auth('column-create'))}<li><a data-toggle="modal" data-target="#column-create" data-action="{:url('@admin/site/column_save')}" data-pid="0" data-path="0"> <i class="icon icon-plus"></i> 添加栏目</a></li>{/if}
            <li><a href="{:url('@admin/site/column_sort', 'siteid='.$site['site_id'])}"> <i class="icon icon-sort"></i> 栏目排序</a></li>
            <li><a href="{:url('@admin/site')}"> <i class="icon icon-sitemap"></i> 站点管理</a></li>
        </ul>
        <!-- 导航中的表单 -->
        <form class="navbar-form navbar-left" role="search">
            <input type="text" class="form-control" placeholder="搜索">
            <button type="submit" class="btn btn-default">搜索</button>
            <button class="btn btn-primary toggleTree" data-value="expandAll" type="button">全部折叠</button>
        </form>
    </div>
</div>
<div class="panel">
    <table class="table treetable table-fixed">
        <thead>
            <tr>
                <th width="50" class="text-center"><input type="checkbox" id="selectBtn"></th>
                <th width="50">ID</th>
                <th>栏目名称</th>
                <th>模块</th>
                <th>标识</th>
                <th width="80" class="text-center">分类</th>
                <th width="80" class="text-center">字段</th>
                <th width="80" class="text-center">添加</th>
                <th width="80" class="text-center">修改</th>
                <th width="80" class="text-center">删除</th>
            </tr>
        </thead>
        <tbody>
            {volist name="list" id="vo"}
            <tr data-tt-id="{$vo['column_id']}" data-tt-parent-id="{$vo['column_pid']}" data-tt-branch="{$vo['column_child']?'true':''}">
                <td class="text-center"><input type="checkbox" name="columnid[]" value="{$vo['column_id']}"></td>
                <td>{$vo['column_id']}</td>
                <td><i class="icon {$vo['column_icon']}"></i> {$vo['column_name']}</td>
                <td>{:isset($vo->module->module_name)?$vo->module->module_name:''}</td>
                <td>{:isset($vo->module->module_path)?$vo->module->module_path:''}</td>
                <td class="text-center">{$vo['column_cate_html']|raw}</td>
                <td class="text-center">
                    {if ($vo['column_field'] == 1)}
                        {if(check_auth('column-param'))}<a href="{:url('@admin/site/param', 'siteid='.$site['site_id'].'&columnid='.$vo['column_id'])}">参数</a>{/if}
                    {/if}
                </td>
                <td class="text-center">{if(check_auth('column-create'))}<a data-toggle="modal" data-target="#column-create" data-action="{:url('@admin/site/column_save')}" data-pid="{$vo['column_id']}" data-path="{$vo['column_path'].','.$vo['column_id']}">添加</a>{/if}</td>
                <td class="text-center">{if(check_auth('column-edit'))}<a data-toggle="modal" data-target="#column-create" data-url="{:url('@admin/site/column_edit', 'id='.$vo['column_id'])}" data-action="{:url('@admin/site/column_update')}">修改</a>{/if}</td>
                <td class="text-center">{if(check_auth('column-delete'))}<a class="delete" data-url="{:url('@admin/site/column_delete','siteid='.$siteid.'&id='.$vo['column_id'])}">删除</a>{/if}</td>
            </tr>
            {/volist}
        </tbody>
    </table>
    {empty name="list"}
    <div class="mb20 mt20 text-center">
        {if ($keywords!='')}
            未搜索到 “{$keywords}” 相关的栏目 &nbsp; <a href="{:url('@admin/site/column', 'siteid='.$site['site_id'])}">查看全部栏目</a>
        {else /}
            暂未添加栏目
        {/if}
    </div>
    {/empty}

    {notempty name="list"}
    <div class="mb10 ml10 mt10">
        {if(check_auth('column-delete'))}<button class="btn btn-danger column-delete" type="button" data-url="{:url('@admin/site/column_delete')}">删除栏目</button>{/if}
        <button class="btn btn-primary toggleTree" data-value="expandAll"  type="button">全部折叠</button>
    </div>
    {/notempty}

</div>
{include file="site/column/create" /}
{/block}


<!-- JS -->
{block name="js"}
    {load href="/static/lib/treetable/jquery.treetable.js"}
    {load href="/static/lib/chosen/chosen.min.js"}
    {load href="/static/lib/chosenicons/zui.chosenicons.min.js"}
    {load href="/static/lib/jquery/jquery.form.js"}
    {load href="/static/lib/jquery/jquery.autofill.min.js"}
    {load href="/static/lib/validator/validator.min.js"}
{/block}
{block name="jscode"}
<script type="text/javascript">
$(document).ready(function() {
    var change_module = function(){
        // 选中项
        var that = $("input[name=column_moduleid]:checked");
        $("#column_cate_div").css('display', that.data('cate') == 1 ? 'block' : 'none');
        $("#column_field_div").css('display', that.data('field') == 1 ? 'block' : 'none');
        $("#column_customer").css('display', (that.data('cate') == 0 && that.data('field') == 0) ? 'none' : 'block');
    }

    $("input[name=column_moduleid]").bind('change', function(event) {
        change_module();
    });

    /** 当show方法被调用时，此事件将被立即触发。 * */
    $("#column-create").on('shown.zui.modal', function(event) {
        var myform = $("#form-column-submit");
        myform.resetForm(); // 表单重置

        var el = $(event.relatedTarget),
            $id = el.attr('data-id'),
            $url = el.attr('data-url'),
            $action = el.attr('data-action');

        // 赋值
        $("input[name=column_pid]").val(el.data('pid'));
        $("input[name=column_path]").val(el.data('path'));

        if ($action != '') {
            myform.attr('action', $action);
        }

        // 获取数据 自动填充到表单
        if ($url != undefined && $url != '') {
            sxj.loading();
            $.getJSON($url, function(json, textStatus) {
                sxj.loading('hide');
                // 使用jquery.autofill.js插件赋值
                myform.autofill(json['data']);
                // 
                $("select[name='column_icon']").data('value', json.data['column_icon']);
                $('select.chosen-icons').trigger('chosen:updated');

                // 触发选中事件
                change_module();

            }).error(function() {
                sxj.msg_error()
            });
        }
    });
    /** ajax提交表单 弹出框 **/
    $('form#form-column-submit').validator().on('submit', function(e) {
        if (e.isDefaultPrevented()) {
            return false;
        }

        var myfrom = $(e.target);
        if (myfrom.attr('action') == '' || myfrom.attr('action') == undefined) {
            sxj.msg_error('未配置表单的 action ');
            return false
        }

        // 使用jquery.form.js插件提交
        $(this).ajaxSubmit({
            beforeSubmit: sxj.loading,
            success: sxj.msg_thinkphp
        });
        return false;
    })
    /**/
    $(".column-delete").click(function(event) {
        var url = $(this).data('url');
        var checksStatus = chk_all();
        var url = url + '?siteid={$siteid}&id=' + checksStatus.join(',');
        if(checksStatus != false){
            bootbox.confirm({
                message: "确定要删除选中的内容？",
                callback: function(result) {
                    if (result) {
                        sxj.ajax_del(url);
                    }
                }
            });
        }
    });
    /**/
    $("#selectBtn").click(function(){        
        $("input[name='columnid[]']").prop("checked",this.checked);    
    });

    var chk_all = function(){
        var ids = $("input[name='columnid[]']:checked"), chk_ids = new Array();
        if(ids.length == 0){
            return sxj.msg_error('请先选择内容！');
        }
        ids.each(function(index, el) {
            chk_ids.push(el.value);
        });
        return chk_ids;
    }
});
</script>
{/block}